import { toISOString } from '@/utils/formatter'
export function filterSearch(formFilter, contxt) {
  if (!formFilter) {
    const keys = Object.keys(contxt.queryForm) || []
    keys.splice(0, 2)
    const filter = contxt.filter
    keys.forEach((k) => {
      const item = filter.find((v) => v.control.includes(k))
      if (item) {
        contxt.queryForm[k] = item.type === 'select' ? [] : null
      }
    })
  } else {
    if (formFilter.type === 'datetimerange') {
      const control = formFilter.control.split('|')
      contxt.queryForm[control[0]] = formFilter.key[0] ? toISOString(formFilter.key[0]) : null
      contxt.queryForm[control[1]] = formFilter.key[1] ? toISOString(formFilter.key[1]) : null
    } else {
      contxt.queryForm[formFilter.control] = formFilter.key
    }
  }
  return contxt
}

export function filterSearchAll(formList, contxt) {
  const formFilter = contxt.filter
  const keys = Object.keys(formList)
  formFilter.forEach((v) => {
    if (keys.includes(v.control)) {
      const item = formList[v.control]
      if (v.type === 'datetimerange') {
        const control = (v.control || []).split('|')
        contxt.queryForm[control[0]] = item[0] ? toISOString(item[0]) : null
        contxt.queryForm[control[1]] = item[1] ? toISOString(item[1]) : null
      } else {
        contxt.queryForm[v.control] = item
      }
    }
  })
  return contxt
}
